home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / comm2 / mmstrdsc.lha / MM / Rexx / MM_SetAreaDesc.rexx < prev    next >
OS/2 REXX Batch file  |  1996-04-28  |  10KB  |  515 lines

  1. /*
  2.  
  3.                    $VER: MM_SetAreaDesc.rexx 0.25  (28.04.96)
  4.  
  5.                            (C) 1995/96 Robert Hofmann
  6.  
  7. */
  8.  
  9. parse arg opts
  10.  
  11. options cache
  12. options failat 99
  13. options results
  14.  
  15. signal on break_c
  16. signal on break_d
  17. signal on break_e
  18. signal on break_f
  19. signal on halt
  20. signal on ioerr
  21. signal on syntax
  22.  
  23. address 'MAILMANAGER'
  24.  
  25.  
  26. Main:
  27.  
  28.     call Init
  29.     call Parse_Args(opts)
  30.     call Header
  31.     call Set_Desc
  32.  
  33. call Quit(0, 'All done.')
  34. exit
  35.  
  36.  
  37. break_c:; break_d:; break_e:; break_f:; halt:
  38.  
  39.     signal off break_c
  40.     signal off break_d
  41.     signal off break_e
  42.     signal off break_f
  43.     signal off halt
  44.  
  45.     return_code        =    5
  46.     error_line    = 0
  47.     error_msg            = 'Execution halted!!!'
  48.     rc                        = 0
  49. signal Exit
  50.  
  51.  
  52. Exit:
  53.  
  54.     select
  55.         when return_code>=40 then error = 'INTERNAL-ERROR:'
  56.         when return_code>=30 then error = 'IO-ERROR:'
  57.         when return_code>=20 then error = 'ERROR:'
  58.         when return_code>=10 then error = 'WARNING:'
  59.         when return_code>=5  then error = 'INFO:'
  60.         otherwise                                    error = ''
  61.     end
  62.  
  63.   call Log()
  64.     call Log('***' strip(error error_msg) '***', '+')
  65.     call Log(,'\')
  66.  
  67.     call setclip('MM_LogPre', system.mm.logpre)
  68.  
  69. exit return_code
  70.  
  71.  
  72. Expand_Path: procedure
  73.  
  74.     parse arg path
  75.  
  76.     if pos(':', path)+pos('/', path)=0 then path = path(pragma('d')) || path
  77.  
  78. return path
  79.  
  80.  
  81. Get_Arg: procedure Expose args system.
  82.  
  83.   arg keyword, mode, old
  84.  
  85.     uargs    = upper(args)
  86.     p            = find(uargs, keyword)
  87.  
  88.     if p=0    then
  89.         do
  90.             p = pos(' 'keyword'=', ' 'uargs)
  91.  
  92.             if p>0 then    args    = overlay(' ', args, p+length(keyword))
  93.  
  94.       p = find(upper(args), keyword)
  95.         end
  96.  
  97.     system.cmdopt.keyword    = p>0
  98.  
  99.     select
  100.         when mode=0    then
  101.             if p>0 then
  102.                 do
  103.                     ret        = 1
  104.                     args    = delword(args, p, 1)
  105.                 end
  106.             else ret    = old
  107.  
  108.         when mode=1 then
  109.             if p>0 then
  110.                 do
  111.                     left    = subword(args, 1, p-1)
  112.                     rest    = subword(args, p+1)
  113.  
  114.                     if left(rest, 1)='"' then    parse var rest . '"'    ret '"'    rest
  115.                     else                                            parse var rest                ret            rest
  116.  
  117.                     args    = strip(left strip(rest))
  118.                 end
  119.             else ret    = old
  120.  
  121.         when mode=2 then
  122.             do
  123.                 if left(args, 1)='"'    then    parse var args . '"'    ret '"'    args
  124.                 else                                                parse var args                ret         args
  125.  
  126.                 if strip(ret)=''            then    ret = old
  127.             end
  128.  
  129.         otherwise exit 99
  130.     end
  131.  
  132.     args    = strip(args)
  133.     ret        = strip(ret, 'b', '" ')
  134.  
  135. return ret
  136.  
  137.  
  138. Get_Version: procedure
  139.  
  140.     parse arg mode
  141.  
  142.     parse value sourceline(3-mode) with . . ver .
  143.     parse var ver tst 'ß' .
  144.  
  145.     if ~datatype(strip(tst, 'b', '/ce '), 'N') then
  146.         if ~mode then ver = Get_Version(1)
  147.         else exit 99
  148.  
  149. return ver
  150.  
  151.  
  152. Header:
  153.  
  154.     call Log(,'/')
  155.     call Log('***' system.prg.id '***', '+')
  156.     call Log('  'system.prg.cr)
  157.     call Log()
  158.  
  159. return
  160.  
  161.  
  162. Init:
  163.  
  164.     system.                        = 0
  165.  
  166.     MM_GetTaskPri                'system.taskpri'
  167.     call                                pragma('p', system.taskpri)
  168.  
  169.     system.prg.name        = 'MM_SetAreaDesc'
  170.     system.prg.ver        = Get_Version(0)
  171.     system.prg.id            = system.prg.name 'v'system.prg.ver
  172.     system.prg.cr            = '(C) 1995/96 Robert Hofmann'
  173.     system.mm.logpre  = getclip('MM_LogPre')
  174.     system.prg.logpre = system.mm.logpre'|'
  175.     system.status            = ''
  176.     call                   setclip('MM_LogPre', system.prg.logpre)
  177.     system.cmdopts        = 'OLDCFG/A,NEWCFG/A,DESCFILE/A,FORCE/S,QUIET/S,INSERT/S,REPLACE/S'
  178.  
  179.     call Include_Lib('rexxsupport')
  180. return
  181.  
  182.  
  183. Include_Lib: procedure
  184.  
  185.     parse arg lib, prio
  186.     if right(upper(lib), 8)~='.LIBRARY' then lib=lib'.library'
  187.     if prio='' then prio=0
  188.  
  189.     if ~show('l', lib) then
  190.         if ~addlib(lib, prio, -30, 0) then
  191.             do
  192.                 say '*** ERROR: Could not open' lib'!!! ***'
  193.                 exit 10
  194.             end
  195. return
  196.  
  197.  
  198. IOerr:
  199.  
  200.     signal off ioerr
  201.  
  202.     return_code        = 20
  203.     error_line    = sigl
  204.     error_msg            = 'IO-error' rc 'at line' sigl '['errortext(rc)']')
  205.     rc                        = 0
  206. signal Exit
  207.  
  208.  
  209. Log: procedure Expose system.
  210.  
  211.     parse arg text, pre
  212.  
  213.     tmp        = word('PRG MM', (pre~='')+1)
  214.     text    = system.tmp.logpre || pre' 'text
  215.  
  216.     MM_WriteLog 'text' '2'
  217. return
  218.  
  219.  
  220. Path: procedure
  221.  
  222.     parse arg path
  223.  
  224.     tmp    = right(path, 1)
  225.  
  226.     if tmp~='/' & tmp~=':' then path = path'/'
  227. return path
  228.  
  229.  
  230. Parse_Args: procedure Expose system.
  231.  
  232.     parse arg args
  233.  
  234.     tpl        = system.cmdopts',?/S'
  235.     args    = translate(args, ' ', '9'x)
  236.  
  237.     pk        = pos('/K', tpl)
  238.     ps        = pos('/S', tpl)
  239.  
  240.     select
  241.         when pk=0    & ps=0    then    p    = 0
  242.         when pk=0 & ps>0    then  p    = ps
  243.         when ps=0 & pk>0    then    p    = pk
  244.         otherwise                                p    = min(pk, ps)
  245.     end
  246.  
  247.     p            = lastpos(',', left(tpl, p))
  248.     tpl        = substr(tpl',', p+1) || left(tpl, max(p-1, 0))
  249.  
  250.     do while tpl~=''
  251.         parse var tpl template ',' tpl
  252.         parse var template keyword '/' .
  253.  
  254.         bool    = pos('/S',    template)>0
  255.         key        = pos('/K', template)>0
  256.         must    = pos('/A', template)>0
  257.         num        = pos('/N', template)>0
  258.  
  259.         select
  260.             when must then        system.arg.keyword    = '0'x
  261.             when bool    then        system.arg.keyword    = 0
  262.             when num    then        system.arg.keyword    = 0
  263.  
  264.             otherwise                    system.arg.keyword    = ''
  265.         end
  266.  
  267.         if bool | key    then    mode    = ~bool
  268.         else                                mode    = 2
  269.  
  270.         system.arg.keyword    = Get_Arg(keyword, mode, system.arg.keyword)
  271.  
  272.         if keyword='?' & system.arg.keyword=1 then leave
  273.  
  274.         if must & system.arg.keyword='0'x then
  275.             do
  276.                 tmp    = template 'missing!!!'
  277.  
  278.                 say
  279.                 say ' ***' tmp '***'
  280.  
  281.                 signal Usage
  282.             end
  283.  
  284.         if num & ~datatype(system.arg.keyword, 'N') then
  285.             if ~must & system.arg.keyword='' then    system.arg.keyword    = 0
  286.             else
  287.                 do
  288.                     tmp    = 'Numeric value expected for' template', but is "'system.arg.keyword'"!!!'
  289.  
  290.                     say
  291.                     say ' ***' tmp '***'
  292.  
  293.                     signal Usage
  294.                 end
  295.     end
  296.  
  297.     tmp    = '?'; if system.arg.tmp then signal Usage
  298.  
  299.     if args~='' then call Quit(10, 'Unknown option(s):' args)
  300.  
  301.  
  302.     system.arg.descfile    = Expand_Path(system.arg.descfile)
  303.     system.arg.newcfg        = Expand_Path(system.arg.newcfg)
  304.   system.arg.oldcfg        = Expand_Path(system.arg.oldcfg)
  305.  
  306.     if ~exists(system.arg.oldcfg)        then call Quit(30, system.arg.oldcfg         'does not exist!')
  307.  
  308.     if ~system.arg.force then
  309.         if exists(system.arg.newcfg)    then call Quit(31, system.arg.newcfg         'does already exist!')
  310.  
  311.     if ~exists(system.arg.descfile)    then call Quit(32, system.arg.descfile    'does not exist!')
  312.  
  313. return
  314.  
  315.  
  316. Quit:
  317.  
  318.     parse arg return_code, error_msg
  319.  
  320.     error_line    = 0
  321.     rc                        = 0
  322. signal Exit
  323.  
  324.  
  325. Replace: procedure
  326.  
  327.     parse arg string, new, old
  328.  
  329.     do while index(string, old) ~= 0
  330.         interpret "parse var string l '"old"' r"
  331.         string = l || new || r
  332.     end
  333.  
  334. return string
  335.  
  336.  
  337. Request_Choice: procedure Expose system.
  338.  
  339.     parse arg text, buttons, ret_vals
  340.  
  341.     title    = system.prg.name'-Requester'
  342.     text    = translate(Replace(text, '0A'x, '\n'), '1b'x, '\')
  343.  
  344.     if length(text)<40 then text = center(text, 40)
  345.  
  346.     MM_Requester title 'text' 'buttons'
  347.  
  348.     if rc=0 then rc=words(ret_vals)
  349.  
  350. return compress(word(ret_vals, rc), '_')
  351.  
  352.  
  353. Set_Desc: procedure Expose system.
  354.  
  355.     if ~system.arg.quiet then Show
  356.  
  357.     call Log(' Reading' system.arg.oldcfg'...')
  358.  
  359.     MM_ReadStem system.arg.oldcfg 'cfg'
  360.     if RC~=0 then call Quit(34, 'Unable to read' system.arg.oldcfg)
  361.  
  362.     call Log(' Reading' system.arg.descfile'...')
  363.  
  364.     if ~open(in, system.arg.descfile, r) then call Quit(34, 'Unable to open' system.arg.descfile)
  365.  
  366.     desc.    = ''
  367.  
  368.     do until eof(in)
  369.     parse value translate(readln(in), ' ', '9'x) with area description
  370.         area            = upper(strip(area))
  371.         desc.area = strip(description, 'b', 'd'x'" ')
  372.     end
  373.  
  374.     call close(in)
  375.  
  376.     call Log(' Analysing...')
  377.  
  378.     do n=0 to cfg.count-1
  379.         parse value strip(translate(cfg.n, ' ', '9'x)) with key .
  380.  
  381.         if key='' then iterate
  382.  
  383.         typ = find('#ECHOAREA #FECHOAREA #TICKAREA', key)
  384.  
  385.         select
  386.             when typ=0    then iterate n
  387.             when typ=1    then parse var cfg.n . '"' . '"' area.name .
  388.             when typ=2    then parse var cfg.n . '"' . '"' area.name .
  389.             when typ=3    then parse var cfg.n . area.name .
  390.             otherwise exit 999
  391.         end
  392.  
  393.         if typ~=3 then
  394.             do
  395.                 offs = 4
  396.                 MM_GetAreaInfo            area.name 'area.data'
  397.             end
  398.         else
  399.             do
  400.                 offs = 1
  401.                 MM_GetTickAreaInfo    area.name 'area.data'
  402.       end
  403.  
  404.         if RC~=0 then iterate
  405.  
  406.         call Log('  Processing area' area.name)
  407.  
  408.         old_desc    = area.data.desc
  409.         u_area        = upper(area.name)
  410.         new_desc    = desc.u_area
  411.  
  412.         if old_desc=new_desc | new_desc=''    then iterate
  413.         if system.arg.insert & old_desc~=''            then iterate
  414.  
  415.         add                = 'a'x
  416.         line            = -1
  417.  
  418.         do m=n+offs while strip(cfg.m)~=''
  419.             parse var cfg.m key value
  420.  
  421.             if strip(key)~='Desc' then iterate
  422.  
  423.             add        = ''
  424.             line    = m
  425.             leave
  426.         end
  427.  
  428.         if line=-1            then line    = m
  429.  
  430.         if system.arg.quiet then set    = 1
  431.         else
  432.             do
  433.                 txt = '\nArea    :' area.name'\nOld desc:' old_desc'\nNew Desc:' new_desc'\n'
  434.                 ret = Request_Choice(txt, ' S_KIP  | _QUIT | _ALL |*  _SET  ', '0 3 2 1')
  435.  
  436.                 select
  437.                     when ret=0 then set = 0
  438.                     when ret=1 then set = 1
  439.                     when ret=2 then
  440.                         do
  441.                             system.all        = 1
  442.                             system.arg.quiet    = 1
  443.                             set                        = 1
  444.                         end
  445.                     when ret=3 then call Quit(5, 'Aborted by user.')
  446.                     otherwise exit 999
  447.                 end
  448.             end
  449.  
  450.         if set then
  451.             do
  452.                 call Log('   ->' new_desc)
  453.                 cfg.line = 'Desc      "'new_desc'"'add
  454.             end
  455.  
  456.         n = line
  457.     end
  458.  
  459.     call Log(' Writing' system.arg.newcfg'...')
  460.  
  461.     MM_WriteStem system.arg.newcfg 'cfg'
  462.  
  463.     if system.all then system.arg.quiet = 0
  464.  
  465.     if system.arg.replace then
  466.         do
  467.             rep    = 1
  468.  
  469.             if ~system.arg.quiet then rep    = Request_Choice('\c\nShall I replace the',
  470.                 'configuration-file\n\n'system.arg.oldcfg'\n\nwith\n\n'system.arg.newcfg'\n\n???',,
  471.                 ' _NO  |* _YES ', '0 1')
  472.  
  473.             if ~rep then break
  474.  
  475.             call Log(' Replacing' system.arg.oldcfg 'with' system.arg.newcfg'...')
  476.  
  477.             MM_MoveFile system.arg.oldcfg system.arg.oldcfg
  478.             MM_MoveFile system.arg.newcfg system.arg.oldcfg
  479.  
  480.             call Log(' Reloading config...')
  481.  
  482.             MM_LoadCfg
  483.         end
  484.  
  485.     if ~system.arg.quiet then call Request_Choice('   All done.   ', '* _Ok ', '0')
  486.  
  487. return
  488.  
  489.  
  490. Syntax:
  491.  
  492.     signal off syntax
  493.  
  494.     return_code        = 40
  495.     error_line    = sigl
  496.     error_msg            = 'Syntax-error' rc 'at line' sigl '['errortext(rc)']'
  497.     rc                        = 0
  498. signal Exit
  499.  
  500.  
  501. Usage:
  502.  
  503.     rx.            = ''
  504.     rx.0.0    = '[rx] '
  505.     rx.0.1    = '[.rexx]'
  506.     m                =    pos('/e', system.prg.ver)>0
  507.     tmp            = rx.m.0 || system.prg.name || rx.m.1
  508.  
  509.     say
  510.     say 'Usage:' tmp system.cmdopts
  511.     say
  512. call Quit(0, 'Usage requested.')
  513.  
  514.  
  515.